﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.datatype.controls;
using umbraco.cms.businesslogic.media;
using umbraco.BasePages;

namespace uTube.Core.Umbraco.usercontrols
{
	public partial class post_install : UserControl
	{

		// -------------------------------------------------------------------------
		// Constants
		// -------------------------------------------------------------------------

		private static readonly int TRUE_FALSE_DATATYPE_ID = -49;
		private static readonly int TEXTSTRING_DATATYPE_ID = -88;
		private static readonly int TEXTBOX_MULTIPLE_DATATYPE_ID = -89;

		/// <summary>
		/// Handles the Load event of the Page control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
			// Setup a flag to use for checking if MediaType exists
			bool mediaTypeExist = false;

			// Loop through mediatypes to check if a mediatype already exists with alias "uTubeVideoUpload"
			foreach (MediaType mt in MediaType.GetAllAsList())
			{
				if (mt.Alias == "uTubeVideoUpload")
				{
					mediaTypeExist = true;  // Found the MediaType - set flag to true
					break;                  // Stop the loop
				}
			}

			// Only create mediatype if flag is false (does not already exist)
			if (mediaTypeExist == false)
			{
				// Setup default value for uTubeUploadDataTypeID
				int uTubeUploadDataTypeID = 0;

				// Check to see if our Custom Upload datatype exists. If so grab its ID
				foreach (DataTypeDefinition dtd in DataTypeDefinition.GetAll())
				{
					if (dtd.Text == "uTube Video Uploader")
					{
						uTubeUploadDataTypeID = dtd.Id;     // Found the DataType - set the ID
						break;                              // Stop the loop
					}
				}


				// Check if uTubeUploadDataTypeID NOT equal to 0 (Means we found the ID of the datatype)
				if (uTubeUploadDataTypeID != 0)
				{
					// -------------------------------------------------------------------------
					// Add MediaType, as packager does not allow this function yet
					// -------------------------------------------------------------------------

					// Use adminUser to create media type
					User adminUser = new User(0);

					// Create new Media Type using adminUser
					MediaType uTubeMediaType = MediaType.MakeNew(adminUser, "uTubeVideoUpload");

					// Create Tab 'Video' for properties to sit on
					int tabId = uTubeMediaType.AddVirtualTab("Video");

					// Add properties
					uTubeMediaType.AddPropertyType(new DataTypeDefinition(uTubeUploadDataTypeID), "uploader", "Video");                                     // Custom Upload datatype
					uTubeMediaType.AddPropertyType(new DataTypeDefinition(TEXTSTRING_DATATYPE_ID), "uTubeVideoTitle", "Video Title");                       // Textstring (Mandatory)
					uTubeMediaType.AddPropertyType(new DataTypeDefinition(TEXTBOX_MULTIPLE_DATATYPE_ID), "uTubeVideoDescription", "Video Description");     // Textbox multiple (Mandatory)
					uTubeMediaType.AddPropertyType(new DataTypeDefinition(TEXTSTRING_DATATYPE_ID), "uTubeKeywords", "Keywords");                            // Textstring (Mandatory)
					uTubeMediaType.AddPropertyType(new DataTypeDefinition(TRUE_FALSE_DATATYPE_ID), "uTubePrivateVideo", "Private Video");                   // True/False            

					// Set the properties that need to be mandatory
					uTubeMediaType.getPropertyType("uTubeVideoTitle").Mandatory = true;
					uTubeMediaType.getPropertyType("uTubeVideoDescription").Mandatory = true;
					uTubeMediaType.getPropertyType("uTubeKeywords").Mandatory = true;

					// Set properties to the 'Video' tab
					uTubeMediaType.SetTabOnPropertyType(uTubeMediaType.getPropertyType("uploader"), tabId);
					uTubeMediaType.SetTabOnPropertyType(uTubeMediaType.getPropertyType("uTubeVideoTitle"), tabId);
					uTubeMediaType.SetTabOnPropertyType(uTubeMediaType.getPropertyType("uTubeVideoDescription"), tabId);
					uTubeMediaType.SetTabOnPropertyType(uTubeMediaType.getPropertyType("uTubeKeywords"), tabId);
					uTubeMediaType.SetTabOnPropertyType(uTubeMediaType.getPropertyType("uTubePrivateVideo"), tabId);

					// Set other common stuff
					uTubeMediaType.Text = "YouTube Video (Upload)";     // Set the Text of Media Type (Friendly name)
					uTubeMediaType.IconUrl = "mediaMovie.gif";             // Set the icon of Media Type            
					uTubeMediaType.Save();                                  // Save the Media Type    
				}
			}
		}

		/// <summary>
		/// Gets the uTube logo.
		/// </summary>
		/// <returns>Returns the path to the uTube logo.</returns>
		protected string GetLogo()
		{
			return this.Page.ClientScript.GetWebResourceUrl(typeof(uTube.Core.Umbraco.usercontrols.post_install), "uTube.Core.Resources.Images.PrevalueEditor_uTubeLogo.png");
		}
	}
}
